package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.TermDao;
import com.example.domain.Term;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.TermService;
import com.example.tool.DateTool;

/**
 * @author 袁小弘
 * @describe 学期服务层接口实现
 * @date 2020-01-01
 */
@Service
public class TermServiceImpl implements TermService {

    @Autowired
    TermDao termDao;

    /**
     * @describe 添加学期信息
     */
    @Override
    public Result addTerm(Term term) {
        if (term != null) {
            //获取系统当前时间作为记录创建时间
            term.setRecordTime(DateTool.getTimestamp());
        }
        //调用JPA保存实体方法
        Term term1 = termDao.save(term);

        if (term1 != null) {
            return ResultFactory.buildSuccessResult(term1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新学期信息
     */
    @Override
    public Result updateTerm(Term term) {

        if (term != null) {
            //获取系统当前时间作为记录更新时间
            term.setUpdateTime(DateTool.getTimestamp());
        }
        //调用JPA保存并刷新实体方法
        Term term1 = termDao.saveAndFlush(term);

        if (term1 != null) {
            return ResultFactory.buildSuccessResult(term1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除学期信息
     */
    @Override
    public Result deleteTermById(String termId) {

        int row = termDao.deleteByTermId(Integer.parseInt(termId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有学期信息
     */
    @Override
    public Result queryAllTerm() {

        List<Term> list = termDao.findAll();

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询学期信息
     */
    @Override
    public Result queryTermById(String termId) {

        List<Term> list = termDao.findByTermId(Integer.parseInt(termId));

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加学期信息
     */
    @Override
    public Result addTermByExcel(List<Term> list) {
        List<Term> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = termDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 学期信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Term> list = null;
        if (info[0].equals("termName")) {

            list = termDao.findByTermNameLike("%" + info[1] + "%");

        } else if (info[0].equals("termDuration")) {

            list = termDao.findByTermDurationLike("%" + info[1] + "%");

        }
        System.out.println(info.toString() + "like==========" + list);

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据学期编号批量删除学期信息
     */
    @Override
    public Result deleteTermByIds(Map<String, String> termIds) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (termIds != null) {
            String ids = termIds.get("termIds");
            String[] id = ids.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = termDao.deleteByTermIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有学期名称和ID
     */
    @Override
    public Result queryAllTermNameAndId() {
        List<Map<String, Object>> map = termDao.findAllTermNameAndId();

        if (map != null) {
            return ResultFactory.buildSuccessResult(map);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
